#include "SNPosReader.h"
#include "opencv2\core\core_c.h"
#include "opencv2\core\core.hpp"
#include <QFile>
#include <QTextStream>
#include <QStringList>
#include <QDir>
#include "opencv2\highgui\highgui.hpp"
#include "opencv2\imgproc\imgproc.hpp"
//--------------------------------------------------------
SNPosImageReader::SNPosImageReader()
{
}
//--------------------------------------------------------

SNPosImageReader::~SNPosImageReader()
{
}
//--------------------------------------------------------

bool SNPosImageReader::Create(const QString filename, cv::Size win_size)
{
	QFile dirs_file_name(filename);
	if (dirs_file_name.open(QIODevice::ReadOnly))
	{
		QTextStream ts(&dirs_file_name);

		QStringList neg_dirs = ts.readAll().split("\r\n");

		for (auto dir_name : neg_dirs)
		{
			QStringList filters;
			filters << "*.bmp";
			filters << "*.jpg";
			filters << "*.png";

			QDir dir(dir_name);

			QStringList files = dir.entryList(filters);

			for (auto f : files)
			{
				QString path = dir_name + "/" + f;
				
				cv::Mat image = cv::imread(path.toStdString().c_str(), 0);
				cv::resize(image, image, win_size);
				Images.push_back(image);
			}
		}
	}

	return true;
}
//--------------------------------------------------------

bool SNPosImageReader::Get(cv::Mat& img)
{
	img = Images[Last];

	if (Last++ >= Images.size())
		return false;

	return true;
}
//--------------------------------------------------------

void SNPosImageReader::Restart()
{
	Last = 0;
}
//--------------------------------------------------------